/*
 * @(#)Tomdog.java    Aug 23, 2010
 *
 * Copyright 2009 Nyber(Shanghai Itd. All rights reserved.
 */

package com.lab.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Aug 23, 2010 3:25:34 PM
 */

public class Tomdog {

    protected final Log logger = LogFactory.getLog(super.getClass());

    private int port = 8080;

    private Tomdog() {}

    private Tomdog(int port) {
        this.port = port;
    }

    private void doGet(BufferedReader input, OutputStream out) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("doGet... ");
        }
    }

    private void doPost(BufferedReader input, OutputStream out) {}

    private void service() {
        if (logger.isDebugEnabled()) {
            logger.debug("Tomdog running... ");
        }

        try {
            ServerSocket server = new ServerSocket(port);
            while (true) {
                Socket client = server.accept();
                BufferedReader input = new BufferedReader(
                        new InputStreamReader(client.getInputStream()));
                OutputStream out = client.getOutputStream();

                String methodAndProtocal = input.readLine();
                String method = methodAndProtocal.substring(0, 4).trim();
                if ("GET".startsWith(method)) {
                    doGet(input, out);
                } else if ("POST".equalsIgnoreCase(method)) {
                    doPost(input, out);
                }

                client.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Tomdog server = new Tomdog(8080);
        server.service();
    }

}
